package cn.itsource.zcn.pet.service.impl;

import cn.itsource.zcn.org.domain.Employee;
import cn.itsource.zcn.org.domain.Shop;
import cn.itsource.zcn.org.mapper.EmployeeMapper;
import cn.itsource.zcn.org.mapper.ShopMapper;
import cn.itsource.zcn.pet.domain.Pet;
import cn.itsource.zcn.pet.domain.PetDetail;
import cn.itsource.zcn.pet.mapper.PetDetailMapper;
import cn.itsource.zcn.pet.mapper.PetMapper;
import cn.itsource.zcn.pet.query.PetQuery;
import cn.itsource.zcn.pet.service.IPetService;
import cn.itsource.zcn.user.domain.Logininfo;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * (Pet)表服务实现类
 *
 * @author makejava
 * @since 2021-08-23 11:32:20
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetServiceImpl implements IPetService {
    @Autowired
    private PetMapper tPetMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;
    /**
     * 新增数据
     *
     * @param tPet 实例对象
     */
    @Override
    @Transactional
    public void save(Pet tPet, HttpServletRequest request) {
        // 拿出当前登录用户的id
        String token = request.getHeader("token");
        Logininfo logininfo = (Logininfo) redisTemplate.opsForValue().get(token);
        Employee employee = employeeMapper.queryById(logininfo.getUid());
        Shop shop = shopMapper.queryByAdminid(employee.getId());
        //把数据保存
        tPet.setCreatetime(new Date());
        tPet.setType_id(1L);
        tPet.setShop_id(shop.getId());
        tPetMapper.save(tPet);
        // 保存detail表
        PetDetail detail = tPet.getDetail();
        detail.setPet_id(tPet.getId());
        petDetailMapper.save(detail);
    }

    /**
     * 修改数据
     *
     * @param tPet 实例对象
     */
    @Override
    @Transactional
    public void update(Pet tPet) {
        //更新pet
        tPetMapper.update(tPet);
        // 获取detail
        tPet.getDetail();
        // 找出对应所在的peydetail
        PetDetail petDetail = petDetailMapper.queryByPid(tPet.getId());
        // 设置相对应改变的值
        petDetail.setAdoptNotice(tPet.getDetail().getAdoptNotice());
        petDetail.setIntro(tPet.getDetail().getIntro());
        // 更新数据detail
        petDetailMapper.update(petDetail);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    @Transactional
    public void delete(Long id) {
        // 删除pet
        tPetMapper.delete(id);
        // 删除petdetail
        petDetailMapper.deleteByPid(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Pet queryById(Long id) {
        return tPetMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<Pet> queryAll() {
        return tPetMapper.queryAll();
    }

    @Override
    public void deleteBatch(Long[] ids) {

    }

    @Override
    public QueryResult queryPage(PetQuery petQuery, HttpServletRequest request) {
        // 拿出当前登录用户的id
        String token = request.getHeader("token");
        Logininfo logininfo = (Logininfo) redisTemplate.opsForValue().get(token);
        Employee employee = employeeMapper.queryById(logininfo.getUid());
        Shop shop = shopMapper.queryByAdminid(employee.getId());
        // 只能查出当前登录用户的所有pet
        petQuery.setSid(shop.getId());

        Integer count = tPetMapper.count(petQuery);
        List<Pet> page = tPetMapper.page(petQuery);

        return new QueryResult(page,count);
    }

    @Override
    public void onsaleBatch(Long[] ids) {
        tPetMapper.onsaleBatch(ids);
    }

    @Override
    public void offsaleBatch(Long[] ids) {
        tPetMapper.offsaleBatch(ids);
    }

    @Override
    public QueryResult queryPage(PetQuery petQuery) {
        Integer count = tPetMapper.countfront(petQuery);
        List<Pet> page = tPetMapper.pagefront(petQuery);
        return new QueryResult(page,count);
    }

    @Override
    public Pet queryByIdfront(Long id) {
        return tPetMapper.queryByIdfront(id);
    }

}
